﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections.Specialized;
using System.Collections;
public partial class Manage_admin_edit : AdminPageBase
{
    int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        // CheckAdmin();
        GetUrlArgs();
        if (!IsPostBack)
        {
            BindGroup();
            GetUserInfo();
            BindUserPermision();
          

        }
    }
    protected void BindGroup()
    {
        DataSet ds = jtDataAccess.GetList("Group","1=1","GroupId");
        if (CoreTools.CheckDataSet(ds))
        {
            ddlgroup.DataSource = ds;
            ddlgroup.DataTextField = "GroupName";
            ddlgroup.DataValueField = "GroupId";
            ddlgroup.DataBind();
        }
        ListItem item = new ListItem("=请选择角色=","0");
        ddlgroup.Items.Insert(0,item);
    }
    protected void GetUserInfo()
    {
        if (id != 0)
        {
            DataSet ds = new DataSet();
            ds = jtDataAccess.GetUserInfo(id);
            if (CoreTools.CheckDataSet(ds))
            {
                txtusername.Text = ds.Tables[0].Rows[0]["Name"].ToString();
                txtcode.Text = ds.Tables[0].Rows[0]["Code"].ToString();
                ddlvalid.SelectedValue = ds.Tables[0].Rows[0]["IsValid"].ToString();
                ddlgroup.SelectedValue = ds.Tables[0].Rows[0]["GroupId"].ToString();
            }
        }

    }

    protected void GetUrlArgs()
    {
        if (Request.QueryString["id"] == null)
        {
            id = 0;
        }
        else
        {
            id = Convert.ToInt32(Request.QueryString["id"]);
            hidUserId.Value = Request.QueryString["id"].ToString();
        }
    }

    protected void BindUserPermision()
    {
        if (id != 0)
        {
            DataTable dt = jtDataAccess.GetUserPermision(id);
            if (CoreTools.CheckDataTable(dt))
            {
                dgUserPermision.DataSource = dt;
                dgUserPermision.DataBind();
            }

        }
    }


    protected void dgUserPermision_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {

            DropDownList ddr = (DropDownList)e.Item.FindControl("ddlPermision");
            HiddenField hdfPermisionId = (HiddenField)e.Item.FindControl("hdfPermisionId");
            if (ddr != null)
            {
                DataSet ds = new DataSet();
                ds = jtDataAccess.GetList("Permision", "1=1", "PermisionId");
                if (CoreTools.CheckDataSet(ds))
                {
                    ddr.DataSource = ds;
                    ddr.DataTextField = "Name";
                    ddr.DataValueField = "PermisionId";
                    ddr.DataBind();
                    if (hdfPermisionId != null)
                        ddr.SelectedValue = hdfPermisionId.Value;
                }

            }

        }
    }
    protected void dgUserPermision_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            jtDataAccess.Delete("UserPermision", "UserPermisionId", int.Parse(e.CommandArgument.ToString()));
            BindUserPermision();
        }
    }

    protected void btnsave_Click(object sender, EventArgs e)
    {
        int  userid=Convert.ToInt32(hidUserId.Value = Request.QueryString["id"].ToString());
        NameValueCollection nvc = new NameValueCollection();
        nvc.Add("LoginID", txtusername.Text.Trim());
        nvc.Add("Code", txtcode.Text.Trim());
        nvc.Add("Password", CryptorEngine.GetMd5_32(txtpwd.Text.Trim()));
        nvc.Add("GroupId",ddlgroup.SelectedValue);
        if (userid == 0)
        {
            if (jtDataAccess.CheckHave("User", "Name", txtusername.Text.Trim(), 0))
                JsHelper.Alert("改用户名已存在，请用另一用户名！");
            else
            {
                int flag = 0;
                flag=jtDataAccess.AddUser(nvc);
                if(flag==1)
                    JsHelper.Alert("添加成功！");
                else
                    JsHelper.Alert("添加失败！");
            }
        }
        else
        {
            if (jtDataAccess.CheckHave("User", "Name", txtusername.Text.Trim(), userid))
                JsHelper.Alert("改用户名已存在，请用另一用户名！");
            else
            {
                nvc.Add("UserId", userid.ToString());
                if(jtDataAccess.UpdateUser(nvc)==1)
                    JsHelper.Alert("修改成功！");
                else
                    JsHelper.Alert("修改失败！");
            }
        }
        if (userid != 0)
        {
            ArrayList listModelACL = new ArrayList();
            for (int i = 0; i < dgUserPermision.Items.Count; i++)
            {
                if (dgUserPermision.Items[i].ItemType == ListItemType.AlternatingItem || dgUserPermision.Items[i].ItemType == ListItemType.Item)
                {
                    HiddenField hdfMenuId = (HiddenField)dgUserPermision.Items[i].FindControl("hdfMenuId");
                    if (hdfMenuId != null)
                    {
                        DropDownList ddlRight = (DropDownList)dgUserPermision.Items[i].FindControl("ddlPermision");
                        int aclAccess = Convert.ToInt32(ddlRight.SelectedValue);
                        NameValueCollection nvc1 = new NameValueCollection();
                        nvc1.Add("UserId", userid.ToString());
                        nvc1.Add("PermisionId", aclAccess.ToString());
                        nvc1.Add("MenuId", hdfMenuId.Value.ToString());
                        jtDataAccess.AddUserPermision(nvc1);
                    }
                }
            }
        }
       
    }
}